﻿namespace ExamV5.StudyInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmUserStudying : Page
    {
        protected Button butExp;
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLDept;
        protected DropDownList DDLQuery;
        protected DropDownList DDLUPDept;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected Label labTip;
        protected LinkButton LBDel;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        protected LinkButton LBSelect;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TextBox txtQuery;
        protected UpdatePanel UPPart;

        protected void butExp_Click(object sender, EventArgs e)
        {
            try
            {
                string cmdText = "";
                string path = base.Server.MapPath("../") + @"\PublicFile\StudyingDataInfo" + this.httpCookie_3.Value + ".csv";
                string str3 = "";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                StreamWriter writer = new StreamWriter(path, true, Encoding.GetEncoding("GB2312"));
                writer.WriteLine("准考证号,考生姓名,所属部门,所属职务,所属岗位,学习任务名称,学习进度,最后登陆IP,最后学习时间");
                cmdText = "select c.CountTime/(a.StudyLongTime*60) as CountStudy,a.ID,a.PlanName,a.StudyLongTime,b.UserGWName,b.Worker,d.DeptName,d.UPID,b.UserCode,b.UserName,c.CountTime,c.LoginIP,c.LastDate from StudyPlanInfo a,UserInfo b,StudyUser c,DeptInfo d  where d.ID=b.deptID and a.ID=c.PlanID and c.UserCode=b.UserCode ";
                if (this.DDLDept.SelectedItem.Value != "0")
                {
                    cmdText = cmdText + " and b.DeptID in (select b.ID from DeptInfo b where " + this.publicFun_0.GetManagDeptID(this.publicFun_0.CheckString(this.DDLDept.SelectedItem.Value)) + ")";
                }
                if (this.txtQuery.Text.Trim() != "")
                {
                    cmdText = cmdText + " and " + this.publicFun_0.CheckString(this.DDLQuery.SelectedItem.Value) + " like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
                }
                if ((this.ViewState["ManagPower"].ToString() == "N") ? !(this.ViewState["AddPower"].ToString() == "N") : true)
                {
                    if (!((this.ViewState["ManagPower"].ToString() == "N") ? !(this.ViewState["AddPower"].ToString() == "Y") : true))
                    {
                        cmdText = cmdText + " and a.InputMan='" + this.httpCookie_0.Value + "'";
                    }
                }
                else
                {
                    cmdText = cmdText + " and a.ID=0";
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('添加或管理学习任务权限未开放！')", true);
                }
                cmdText = cmdText + "  order by " + this.ViewState["SortFiled"].ToString() + " " + this.ViewState["SortType"].ToString() + ",a.ID desc";
                double num4 = 0.0;
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                connection.Open();
                SqlCommand command = new SqlCommand(cmdText, connection);
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    double num2 = Convert.ToDouble(reader["CountTime"].ToString());
                    double num3 = Convert.ToDouble(reader["StudyLongTime"].ToString());
                    double num = ((num2 / 60.0) / num3) * 100.0;
                    num4 = Convert.ToDouble(reader["CountStudy"].ToString().Trim()) * 100.0;
                    str3 = "";
                    str3 = (str3 + reader["UserCode"].ToString().Trim() + "\x00b7,") + reader["UserName"].ToString().Trim() + ",";
                    if (!(reader["UPID"].ToString().Trim() == "0"))
                    {
                        str3 = str3 + this.publicFun_0.GetValues("select DeptName from DeptInfo where ID=" + reader["UPID"].ToString().Trim(), "DeptName") + @"\" + reader["DeptName"].ToString().Trim() + ",";
                    }
                    else
                    {
                        str3 = str3 + reader["DeptName"].ToString().Trim() + ",";
                    }
                    str3 = (((((str3 + reader["Worker"].ToString().Trim() + ",") + reader["UserGWName"].ToString().Trim() + ",") + reader["PlanName"].ToString().Trim() + ",") + num.ToString("0.00") + "%,") + reader["LoginIP"].ToString().Trim() + ",") + reader["LastDate"].ToString().Trim() + ",";
                    writer.WriteLine(str3);
                }
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
                writer.Flush();
                writer.Close();
                this.labTip.Text = " 导出成功！单击【<a href='../FrmDownLoad.aspx?FileName=StudyingDataInfo" + this.httpCookie_3.Value + ".csv' target=_blank><font color=red>下载</font></a>】进行保存";
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BExpErrTip", @"alert('导出数据失败！\n\n请将PublicFile目录设置为Everyone用户可修改权限！')", true);
            }
        }

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex == -1)
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) != 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label label = (Label) e.Row.Cells[7].FindControl("labStudyDX");
                double num2 = Convert.ToDouble(this.DataGridInfo.DataKeys[e.Row.RowIndex]["CountTime"].ToString());
                double num3 = Convert.ToDouble(this.DataGridInfo.DataKeys[e.Row.RowIndex]["StudyLongTime"].ToString());
                double num = ((num2 / 60.0) / num3) * 100.0;
                if (num >= 100.0)
                {
                    label.Enabled = true;
                    if (this.DataGridInfo.DataKeys[e.Row.RowIndex]["StudyXD"].ToString().Replace("&nbsp;", "") != "")
                    {
                        label.Text = "<a href='#' onclick=javascript:ShowXDInfo('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "','" + base.Server.UrlEncode(e.Row.Cells[1].Text) + "')><font color=green>" + label.Text + "</font></a>";
                    }
                    e.Row.Cells[4].Text = "<font color=green>" + num.ToString("0.00") + "%</font>";
                }
                else
                {
                    label.Enabled = false;
                    e.Row.Cells[4].Text = "<font color=red>" + num.ToString("0.00") + "%</font>";
                }
                if (e.Row.Cells[3].Text.Length > Convert.ToInt32(this.ViewState["SubStrLen"].ToString()))
                {
                    e.Row.Cells[3].Text = "<a title='" + e.Row.Cells[3].Text + "'>" + e.Row.Cells[3].Text.Substring(0, Convert.ToInt32(this.ViewState["SubStrLen"].ToString())) + "…</a>";
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (this.ViewState["SortType"].ToString() != " Desc")
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "UserCode":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "UserName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "PlanName":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "CountStudy":
                            e.Row.Cells[4].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "LastDate":
                            e.Row.Cells[6].Controls.Add(new LiteralControl("↓"));
                            return;
                    }
                    e.Row.Cells[4].Controls.Add(new LiteralControl("↓"));
                }
                else
                {
                    string str = this.ViewState["SortFiled"].ToString();
                    switch (str)
                    {
                        case null:
                            goto Label_0684;

                        case "UserCode":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "UserName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "PlanName":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                            return;
                    }
                    if (!(str == "CountStudy"))
                    {
                        if (!(str == "LastDate"))
                        {
                            goto Label_0684;
                        }
                        e.Row.Cells[6].Controls.Add(new LiteralControl("↑"));
                    }
                    else
                    {
                        e.Row.Cells[4].Controls.Add(new LiteralControl("↑"));
                    }
                }
            }
            return;
        Label_0684:
            e.Row.Cells[4].Controls.Add(new LiteralControl("↑"));
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (!(this.ViewState["SortFiled"].ToString() == sortExpression))
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            else if (!(this.ViewState["SortType"].ToString() == " Desc"))
            {
                this.ViewState["SortType"] = " Desc";
            }
            else
            {
                this.ViewState["SortType"] = " ASC";
            }
            this.method_0();
        }

        protected void DDLDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DDLUPDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DDLDept.Items.Clear();
            this.publicFun_0.LoadDeptInfo(this.DDLDept, this.DDLUPDept.SelectedItem.Value, this.httpCookie_0.Value, "B");
            if (this.DDLDept.Items.Count == 0)
            {
                if (!(this.DDLUPDept.SelectedItem.Value == "0"))
                {
                    ListItem item = new ListItem(this.DDLUPDept.SelectedItem.Text, this.DDLUPDept.SelectedItem.Value);
                    this.DDLDept.Items.Add(item);
                }
                else
                {
                    ListItem item2 = new ListItem("--所有部门--", "0");
                    this.DDLDept.Items.Insert(0, item2);
                }
            }
        }

        protected void LBDel_Click(object sender, EventArgs e)
        {
            int num = 0;
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    num++;
                    string strSql = "";
                    string str = "";
                    string str2 = "";
                    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                    connection.Open();
                    SqlCommand command = new SqlCommand("select StudyUserCode,StudyUserName from StudyPlanInfo where ID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString(), connection);
                    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    if (reader.Read())
                    {
                        str = reader["StudyUserCode"].ToString();
                        str2 = reader["StudyUserName"].ToString();
                    }
                    reader.Close();
                    command.Dispose();
                    connection.Close();
                    connection.Dispose();
                    str = str.Replace("," + this.DataGridInfo.Rows[i].Cells[1].Text.Trim() + ",", ",");
                    str2 = str2.Replace(",[" + this.DataGridInfo.Rows[i].Cells[1].Text.Trim() + "]" + this.DataGridInfo.Rows[i].Cells[2].Text.Trim() + ",", ",");
                    strSql = "delete StudyUser where PlanID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + " and UserCode='" + this.DataGridInfo.Rows[i].Cells[1].Text.Trim() + "'";
                    strSql = strSql + ";update StudyPlanInfo set StudyUserCode='" + str + "',StudyUserName='" + str2 + "' where ID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString();
                    strSql = strSql + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('删除学员[" + this.DataGridInfo.Rows[i].Cells[1].Text.Trim() + "]" + this.DataGridInfo.Rows[i].Cells[2].Text.Trim() + "的学习进度信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
                    this.publicFun_0.ExecuteSql(strSql);
                }
            }
            if (this.DataGridInfo.Rows.Count == num)
            {
                this.DataGridInfo.PageIndex = 0;
            }
            this.method_0();
            this.LBSelect.Text = "选中当页";
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        protected void LBSelect_Click(object sender, EventArgs e)
        {
            int num;
            CheckBox box;
            if (this.LBSelect.Text != "选中当页")
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    box.Checked = false;
                }
                this.LBSelect.Text = "选中当页";
            }
            else
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    if (box.Enabled)
                    {
                        box.Checked = true;
                    }
                }
                this.LBSelect.Text = "取消所选";
            }
        }

        private void method_0()
        {
            string selectCommandText = "";
            selectCommandText = "select c.StudyXD,c.CountTime/(a.StudyLongTime*60) as CountStudy,a.ID,a.PlanName,a.StudyLongTime,b.UserCode,b.UserName,c.CountTime,c.LoginIP,c.LastDate from StudyPlanInfo a,UserInfo b,StudyUser c  where a.ID=c.PlanID and c.UserCode=b.UserCode ";
            if (this.DDLDept.SelectedItem.Value != "0")
            {
                selectCommandText = selectCommandText + " and b.DeptID in (select b.ID from DeptInfo b where " + this.publicFun_0.GetManagDeptID(this.publicFun_0.CheckString(this.DDLDept.SelectedItem.Value)) + ")";
            }
            if (this.txtQuery.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and " + this.publicFun_0.CheckString(this.DDLQuery.SelectedItem.Value) + " like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
            }
            if (!((this.ViewState["ManagPower"].ToString() == "N") ? !(this.ViewState["AddPower"].ToString() == "N") : true))
            {
                selectCommandText = selectCommandText + " and a.ID=0";
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('添加或管理学习任务权限未开放！')", true);
            }
            else if (!((this.ViewState["ManagPower"].ToString() == "N") ? !(this.ViewState["AddPower"].ToString() == "Y") : true))
            {
                selectCommandText = selectCommandText + " and a.InputMan='" + this.httpCookie_0.Value + "'";
            }
            selectCommandText = selectCommandText + "  order by " + this.ViewState["SortFiled"].ToString() + " " + this.ViewState["SortType"].ToString() + ",a.ID desc";
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (Convert.ToInt32(ConfigurationManager.ConnectionStrings["OnlineNum"].ConnectionString) != 0x3a)
                {
                    if (!base.IsPostBack)
                    {
                        if (this.publicFun_0.ReturnPaperData() == "Yes")
                        {
                            this.LBDel.Attributes.Add("onclick", "javascript:{if(confirm('确定要删除学员学习进度？')==false) return false;}");
                            string str = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='SubStrLength'", "ParmValue").Trim();
                            if (str.Trim() == "")
                            {
                                str = "20";
                            }
                            this.ViewState["SubStrLen"] = str;
                            this.txtQuery.Attributes.Add("onkeydown", "SubmitKeyClick('butQuery');");
                            this.ViewState["SortFiled"] = "CountStudy";
                            this.ViewState["SortType"] = " Desc";
                            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                            connection.Open();
                            SqlCommand command = new SqlCommand("select AddStudyPlan,ManagStudyPlan from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                            if (reader.Read())
                            {
                                this.ViewState["AddPower"] = reader["AddStudyPlan"].ToString();
                                this.ViewState["ManagPower"] = reader["ManagStudyPlan"].ToString();
                            }
                            reader.Close();
                            command.Dispose();
                            connection.Close();
                            connection.Dispose();
                            this.DDLUPDept.DataTextField = "DeptName";
                            this.DDLUPDept.DataValueField = "ID";
                            if (!(this.httpCookie_2.Value == "C"))
                            {
                                this.DDLUPDept.DataSource = this.publicFun_0.RetrunDataView("select ID,DeptName from DeptInfo where ID=" + this.httpCookie_3.Value + " order by SortInfo,ID");
                            }
                            else
                            {
                                this.DDLUPDept.DataSource = this.publicFun_0.RetrunDataView("select ID,DeptName from DeptInfo where UPID=0  order by SortInfo,ID");
                            }
                            this.DDLUPDept.DataBind();
                            if (this.httpCookie_2.Value == "C")
                            {
                                this.DDLUPDept.SelectedIndex = -1;
                                this.DDLDept.SelectedIndex = -1;
                                ListItem item2 = new ListItem("--所有单位--", "0");
                                ListItem item3 = new ListItem("--所有部门--", "0");
                                this.DDLDept.Items.Insert(0, item3);
                                this.DDLUPDept.Items.Insert(0, item2);
                            }
                            else
                            {
                                this.DDLDept.Items.Clear();
                                this.publicFun_0.LoadDeptInfo(this.DDLDept, this.DDLUPDept.SelectedItem.Value, this.httpCookie_0.Value, "B");
                                if (this.DDLDept.Items.Count == 0)
                                {
                                    ListItem item = new ListItem(this.DDLUPDept.SelectedItem.Text, this.DDLUPDept.SelectedItem.Value);
                                    this.DDLDept.Items.Add(item);
                                }
                            }
                            this.method_0();
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                        }
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BTHB", "alert('该版本为特惠版，此功能未开放！')", true);
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

